<?php
/**
 * Smart Library - Smart Framework
 * http://smart-framework.highet.eu
 *
 * @category   SmartL
 * @package    SmartL_Zend
 * @subpackage Form
 * @copyright  Copyright (c) 2005-2008 Smart Framework team - Stepan Sindelar, Vojtech Vit
 * @license    http://www.opensource.org/licenses/bsd-license.php	New BSD License
 */

/**
 * @see SmartL_Zend_Translate
 */
require_once 'SmartL/Zend/Translate.php';

/**
 * Library class for easy creating of Zend forms that supports all SmartL additional features.
 *
 * <code>
 * <?php
 * 	$form = SmartL_Zend_Form::getForm(); //for standard Zend_Form with validator errors.
 * 	$form = SmartL_Zend_Form::getForm($config, $myTranslate); //for Zend_Form with user
 * 	//defined config and user defined translation merged with framework's default validator errors translations.
 * 	$form = SmartL_Zend_Form::getForm($config, null, 'Zend_Dojo_Form');	//for Zend_Dojo_Form
 * ?>
 * </code>
 *
 * @package SmartL_Zend
 * @subpackage Form
 */
class SmartL_Zend_Form {
	private function __construct() {
	}
	
	/**
	 * Path to file with validator errors.
	 * If it is not set, default framework's language file with vlidator errors will be used.
	 * 
	 * This can be changed, but before any usage of static method getForm.
	 *
	 * @var string
	 */
	public static $errorsTranslateFilename = null;
	
	/**
	 * Name of Zend_Translate_Adapter to be used for loading validator errors language file.
	 * Default adapter is tmx.
	 * 
	 * This can be changed, but before any usage of static method getForm.
	 *
	 * @var string
	 */
	public static $errorsTranslateAdapter = 'tmx';
	/**
	 * Translations are loaded trought SmartL_Zend_Translate::createTranslate(), so once
	 * error translations are loaded, they don't need to loaded again.
	 * 
	 * @var boolean
	 */
	private static $_formErrorsLoaded = false;
	
	/**
	 * Creates new form and adds paths to SmartL components into it's plugin loader.
	 * Also adds default SmartL form translation file, if $addTranslation is true.
	 *
	 * @param array $config Form's config.
	 * @param Zend_Translate $translate
	 * @param string $class Form class name. Since ZF 1.6 it can be also 'Zend_Dojo_Form', or your own one.
	 * @param boolean $addErrorsTranslate
	 * @return Zend_Form
	 */
	public static function getForm(array $config = array(), $translate = null, $class = 'Zend_Form', $addErrorsTranslate = true, $application = null) {
		if ($application === null) {
			$application = SmartL_Application::getInstance ();
		}

		//adds SmartL specific paths:
		$ds = DIRECTORY_SEPARATOR;
		// Since Zend_Form_Element_File is implemented in ZF, SF has no currently no own element.
		$config ['elementPrefixPath'] [] = array ('prefix' => 'SmartL_Zend', 'path' => 'SmartL' . $ds . 'Zend', 
			'type' => null);  // with type unset it produces sometimes notices in Zend
				
		//create form
		$form = new $class ( $config );
				
		//translations
		if ($addErrorsTranslate) {
			if (isset ( $translate )) {
				if (strtolower ( $translate->toString () ) !== strtolower ( self::$errorsTranslateAdapter )) {
					throw new Exception ( "Validator errors and form field's names must be the same adapters." );
				}
				//There is only one instance of each adapter, this is directed by SmartL_Zend_Translate, 
				//if the form errors were loaded before we don't need to load them again.
				if (! self::$_formErrorsLoaded) {
					$lang = $application->getLanguages();
					$lang = $lang[0];
					$translate->addTranslation ( self::getErrorsTranslateFilename (), $lang->getZendId() );
					$form->setTranslator ( $translate );
					self::$_formErrorsLoaded = true;
				}
				
				$form->setTranslator ( $translate );
			} else {
				$form->setTranslator ( self::getErrorsTranslate () );
			}
		} elseif (isset ( $translate )) {
			$form->setTranslator ( $translate );
		}
		
		//result
		return $form;
	}
	
	/**
	 * Getter for SmartL_Zend_Form::errorsTranslateFilename.
	 * If this variable is not set up, the default value will be returned.
	 *
	 * @return string
	 */
	public static function getErrorsTranslateFilename() {
		if (! isset ( self::$errorsTranslateFilename )) {
			$ds = DIRECTORY_SEPARATOR;
			self::$errorsTranslateFilename = dirname ( __FILE__ ) . $ds . 'Form' . $ds . 'Data' . $ds . 'ValidatorErrors.tmx';
		}
		
		return self::$errorsTranslateFilename;
	}
	
	/**
	 * Returns Zend_Translate class with validator errors translations
	 * constructed according to static variables.
	 *
	 * @return Zend_Translate
	 */
	public static function getErrorsTranslate() {
		return SmartL_Zend_Translate::createTranslate ( self::$errorsTranslateAdapter, self::getErrorsTranslateFilename () );
	}
}
